package com.jec.module.sysmanage.controller;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Endpoint;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.io.Files;
import com.jec.Bootstrap;
import com.jec.module.extern.NTNMS_ServiceImpl;
import com.jec.module.sysmanage.service.UserService;
import com.jec.utils.Response;

/**
 * Created by jeremyliu on 5/8/16.
 */
@Controller
@RequestMapping(value = "/")
public class RootController {

    @Resource
    private NTNMS_ServiceImpl testService;

    @Resource
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public String main(HttpSession httpSession, Model model){
    	
    	return "index.html";

    	/*
        Integer userId = (Integer)httpSession.getAttribute("userId");
        if(userId == null)
            return "login";
        else {
            Role role = userService.getUserRole(userId);
            model.addAttribute("role", role.toValue());
            return "index";
        }
        */
    }

    @RequestMapping(method = RequestMethod.GET, value = "logout")
    public void logout(HttpServletResponse response, HttpSession httpSession){
        httpSession.removeAttribute("userId");
        try {
            response.sendRedirect("");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    
    @RequestMapping(method = RequestMethod.GET ,value = "property")
    @ResponseBody
    public String property(){
        return Bootstrap.getApplicationPath();
    }

    @RequestMapping(method = RequestMethod.GET ,value = "test")
    public String test(){
//        TestService service = new NTNMS_ServiceImpl();
        Endpoint.publish("http://localhost:8080/test",testService);
        return "";
    }

    @RequestMapping(method = RequestMethod.GET, value = "test/fault" )
    public @ResponseBody
    Response reportPerformance(int rate, String startTime, String endTime){
        return Response.Builder(testService.reportPerformance(rate,startTime,endTime));
    }

    @RequestMapping(method = RequestMethod.GET, value = "test/topo")
    public @ResponseBody
    Response topo(){
        return Response.Builder(testService.topoQuery());
    }
    
    @RequestMapping(method = RequestMethod.POST, value = "loadProperties")
    public @ResponseBody
    Response loadProperties(){
    	try {
    		String text = Files.toString(Bootstrap.getConfigFile(), Charset.defaultCharset());
    		return Response.Builder(text);
    	} catch(Exception e) {
    		return Response.Builder(e.getMessage());
    	}
    }
    
    @RequestMapping(method = RequestMethod.POST, value = "saveProperties")
    public @ResponseBody
    Response saveProperties(String text){
    	try {
    		BufferedWriter writer = Files.newWriter(Bootstrap.getConfigFile(), Charset.defaultCharset());
    		writer.write(text);
    		writer.close();
    		Bootstrap.reset();
    		return Response.Builder();
    	} catch(Exception e) {
    		return Response.Builder().status(Response.STATUS_FAIL);
    	}
    }
    
}
